﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UDPHelper
{
    /// <summary>
    /// UDP消息管理类
    /// （专门用来存储准备发送的消息）
    /// </summary>
    public class UDPPackageManager
    {
        private ConcurrentStack<UDPPackage> UDPPackageStack = new ConcurrentStack<UDPPackage>();
        private List<Action> PushCallbacks = new List<Action>();

        /// <summary>
        /// 将数据压入队列
        /// </summary>
        /// <param name="package"></param>
        public void PushUDPPackageToStack(UDPPackage package)
        {
            UDPPackageStack.Push(package);

            foreach (var item in PushCallbacks)
            {
                item();
            }
        }

        /// <summary>
        /// 弹出消息
        /// </summary>
        /// <returns></returns>
        public bool PopUDPPackageFromStack(out UDPPackage package)
        {
            return UDPPackageStack.TryPop(out package);
        }

        /// <summary>
        /// 压入数据的回调
        /// </summary>
        /// <param name="action"></param>
        public void AddPushCallback(Action action)
        {
            PushCallbacks.Add(action);
        }
    }
}
